package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mapper.MaterialsMapper;
import com.example.mapper.WarehouseMapper;
import com.example.model.dtos.PageResult;
import com.example.model.pojo.Materials;
import com.example.model.pojo.Supplier;
import com.example.model.pojo.Warehouse;
import com.example.model.vo.MaterialsVo;
import com.example.service.MaterialsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class MaterialsServiceImpl implements MaterialsService {

    @Autowired(required = false)
    MaterialsMapper materialsMapper;

    @Autowired(required = false)
    WarehouseMapper warehouseMapper;

    @Override
    public Boolean addMaterials(Materials materials) {
        int insert = materialsMapper.insert(materials);
        return insert != 0;
    }

    @Override
    public Boolean deleteMaterials(String id) {
        int insert = materialsMapper.deleteById(id);
        return insert != 0;

    }

    @Override
    public Boolean updateMaterials(Materials materials) {
        int insert = materialsMapper.updateById(materials);
        return insert != 0;
    }

    @Override
    public PageResult materialsList(Integer pageStart, Integer pageSize) {
        //创建分页对象并将当前页码和页面大小写入
        Page<Materials> materialsPage =new Page<>(pageStart, pageSize);
        QueryWrapper<Materials> queryWrapper = new QueryWrapper<>();
        materialsMapper.selectPage(materialsPage, queryWrapper);
        List<Materials> records = materialsPage.getRecords();

//        返回一个分页后的结果
        PageResult pageResult = new PageResult(pageStart.longValue(), pageSize.longValue(), materialsPage.getTotal(),records);

        return pageResult;
    }

    @Override
    public List<Materials> getAll() {
        List<Materials> materialsList = materialsMapper.selectList(new QueryWrapper<>());
        return materialsList;
    }

    @Override
    public List<MaterialsVo> getAllVo() {
        List<Materials> materialsList = materialsMapper.selectList(new QueryWrapper<>());
        List<Warehouse> warehouses = warehouseMapper.selectList(new QueryWrapper<>());
        HashMap<String, Integer> warehouseMap = new HashMap<>();

        //处理库存数量
        warehouses.stream().forEach(warehouse -> {warehouseMap.put(warehouse.getMaterialsId(), warehouse.getTotal());});

        ArrayList<MaterialsVo> materialsVoArrayList = new ArrayList<>();
        materialsList.stream().forEach(materials -> {
            Integer total = warehouseMap.get(materials.getMyId());
            MaterialsVo materialsVo = new MaterialsVo(materials.getMyId(),materials.getName(),materials.getModel(),materials.getManufacturer(),materials.getPrice(),total==null?0:total);
            materialsVoArrayList.add(materialsVo);
        });

        return materialsVoArrayList;
    }

    @Override
    public List<Warehouse> getWarehouse() {
        List<Warehouse> warehouseList = warehouseMapper.selectList(new QueryWrapper<>());
        return warehouseList;
    }

    @Override
    public Boolean updateWarehouseList(List<Warehouse> warehouseList) {

        warehouseList.forEach(warehouse -> {
            warehouseMapper.updateById(warehouse);
        });
        return true;
    }


}
